package com.chat.view;

import com.chat.Local;
import com.chat.service.UpdateGroupMsg;
import com.chat.io.ChangeImage;
import com.chat.io.SetCircle;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

/*
   修改群信息的Controller类
 */
public class ChangeGroupMsgController {
    public TextField info;
    public TextField name;
    public Label time;
    public Label number;
    public Label id;
    public ImageView touxiang;
    public File file=null;
    public void initialize() {
        id.setText(Local.group.getId());
        name.setText(Local.group.getName());
        number.setText(Local.group.getCount());
        time.setText(Local.group.getTime());
        info.setText(Local.group.getInfo());
        File file1 = new File("D:/chatproject/client/" + Local.group.getId() + ".png");
        touxiang.setImage(new Image("file:" + file1.getAbsolutePath()));
        touxiang.setClip(new SetCircle().getRectangle(40,40));
    }
    public void fanhui(MouseEvent mouseEvent) {
        Stage stage=(Stage) time.getScene().getWindow();
        stage.close();
        try {
            GroupMsgView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void changehead(MouseEvent mouseEvent) {
        FileChooser chooser=new FileChooser();
        chooser.setInitialDirectory(new File("D:\\chatproject\\headimage"));
        chooser.setTitle("选择图片");
        chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PNG","*.png"));
        file=chooser.showOpenDialog(info.getScene().getWindow());
        if(file==null){
            System.out.println("未选择");
        }
        else{
            System.out.println("成功选择");
            System.out.println(file.toURI().toString());
            System.out.println(file.getAbsoluteFile());
            touxiang.setImage(new Image("file:"+file.getAbsolutePath()));
        }
    }//更改群头像

    public void xiugai(MouseEvent mouseEvent) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setHeaderText("恭喜");
        alert.setContentText("成功修改群信息！");
        alert.showAndWait();
        if(file!=null) {
            ChangeImage ci = new ChangeImage();
            byte[] b = ci.get(file);
            ci.set(Local.group.getId(), b);
            Local.group.setHeadpicture(b);
        }
        Local.group.setName(name.getText());
        Local.group.setInfo(info.getText());
        UpdateGroupMsg ugm=new UpdateGroupMsg();
        ugm.updategroupmsg();
    }//更改群信息
}
